package com.yunatt.service.impl;

import com.yunatt.mapper.EnrollInfoMapper;
import com.yunatt.mapper.PersonMapper;
import com.yunatt.model.EnrollInfoEntity;
import com.yunatt.model.PersonEntity;
import com.yunatt.model.vo.UserInfo;
import com.yunatt.service.EnrollInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class EnrollInfoServiceImpl implements EnrollInfoService {

    @Autowired
    private EnrollInfoMapper enrollInfoMapper;

    @Autowired
    private PersonMapper personMapper;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return enrollInfoMapper.deleteById(id);
    }
    @Override
    public int deleteByEnrollId(Long id) {
        return enrollInfoMapper.deleteByEnrollId(id);
    }

    @Override
    public int insert(Long enrollid, int backupnum, String imagPath, String signature) {
        EnrollInfoEntity enrollInfo = new EnrollInfoEntity();
        enrollInfo.setEnrollId(enrollid);
        enrollInfo.setBackupnum(backupnum);
        enrollInfo.setImagePath(imagPath);
        enrollInfo.setSignatures(signature);
        return enrollInfoMapper.insert(enrollInfo);
    }

    @Override
    public int insertSelective(EnrollInfoEntity record) {
        return enrollInfoMapper.insert(record);
    }

    @Override
    public EnrollInfoEntity selectByPrimaryKey(Integer id) {
        return enrollInfoMapper.selectById(id);
    }

    @Override
    public int updateByPrimaryKeySelective(EnrollInfoEntity record) {
        return enrollInfoMapper.updateById(record);
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(EnrollInfoEntity record) {
        return enrollInfoMapper.updateById(record);
    }

    @Override
    public EnrollInfoEntity selectByBackupnum(Long enrollId, int backupnum) {
        return enrollInfoMapper.selectByBackupnum(enrollId, backupnum);
    }

    @Override
    public List<EnrollInfoEntity> selectAll() {
        return enrollInfoMapper.selectAll();
    }

    @Override
    public List<EnrollInfoEntity> selectByEnrollId(Long enrollId) {
        return enrollInfoMapper.selectByEnrollId(enrollId);
    }

    @Override
    public List<UserInfo> usersToSendDevice() {
        List<PersonEntity>persons=personMapper.selectAll();
        List<EnrollInfoEntity>enrollInfos=enrollInfoMapper.selectAll();
        List<UserInfo>userInfos=new ArrayList<UserInfo>();
        for (int i = 0; i < persons.size(); i++) {
            //Person person=new Person();
            for (int j = 0; j < enrollInfos.size(); j++) {
                if(persons.get(i).getId().longValue()==enrollInfos.get(j).getEnrollId().longValue()){
                    UserInfo userInfo=new UserInfo();
                    userInfo.setAdmin(persons.get(i).getRollId());
                    userInfo.setBackupnum(enrollInfos.get(j).getBackupnum());
                    userInfo.setEnrollId(persons.get(i).getId());
                    userInfo.setName(persons.get(i).getName());
                    userInfo.setRecord(enrollInfos.get(j).getSignatures());

                    userInfos.add(userInfo);
                }

            }
        }

        return userInfos;
    }
}
